﻿using CloudStorageLight.SharePointWeb.Controllers;
using CloudStorageLight.SharePointWeb.Models;
using CloudStorageLight.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Http;
using CloudStorageLight.Core.Web;
using System.Web.OData.Extensions;
using System.Web.Http.OData.Extensions;

namespace CloudStorageLight.SharePointWeb
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "createCloudAccount",
                url: "Account/CreateCloudAccount/{id}",
                defaults: new { controller = "Account", action = "CreateCloudAccount", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "HelperError",
                url: "Helper/Error/{id}",
                defaults: new { controller = "Helper", action = "Error", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "ConfigurationOneDriveCallback",
                url: "Settings/ConfigurationOneDriveCallback",
                defaults: new { controller = "Settings", action = "ConfigurationOneDriveCallback" }
            );

            routes.MapRoute(
                name: "ConfigurationGoogleDriveCallback",
                url: "GoogleAuthCallback/{action}",
                defaults: new { controller = "GoogleAuthCallback", action = "IndexAsync" }
            );

            if (SystemSettings.Instance.MultiDomain)
            {
                RegisisterMultiDomain(routes);

            }
            else
            {
                RegisisterSingleDomain(routes);

            }

            routes.MapRoute(
                name: "HomeIndex",
                url: "Home/Index/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "WebPartIndex",
                url: "Home/WebPart/{id}",
                defaults: new { controller = "WebPart", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Root", id = UrlParameter.Optional }
            );

        }


        private static void RegisisterMultiDomain(RouteCollection routes)
        {

            var config = System.Web.Http.GlobalConfiguration.Configuration;

            var builder = DataController.GetModelBuilder("Data");
            var model = builder.GetEdmModel();

            var odataRouteV4 = config.MapODataServiceRoute(
                routeName: "odataV4",
                routePrefix: "({domain})/odata",
                model: model);

            var defaultConventions = System.Web.Http.OData.Routing.Conventions.ODataRoutingConventions.CreateDefault();
            var conventions = new List<System.Web.Http.OData.Routing.Conventions.IODataRoutingConvention>();
            foreach (var convention in defaultConventions)
            {
                if (convention is System.Web.Http.OData.Routing.Conventions.MetadataRoutingConvention)
                {
                    // Don't need to special case these conventions.
                    conventions.Add(convention);
                }
                else
                {
                    conventions.Add(new VersionedRoutingConvention(convention, "V3"));
                }
            }

            //var builder3 = DataV3Controller.GetModelBuilder("Data");
            //var odataRoute = config.Routes.MapODataServiceRoute("odataV3", "({domain})/odatav3", builder3.GetEdmModel(),
             //   new System.Web.Http.OData.Routing.DefaultODataPathHandler(), conventions, new System.Web.Http.OData.Batch.DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));


            routes.MapRoute(
                "files", "({domain})/f/{*id}",
                 new
                 {
                     controller = "Home",    // コントローラ名
                     action = "Index",        // アクション名
                     domain = "",
                 }
            );
            routes.MapRoute(
                "webparts", "({domain})/w/{*id}",
                 new
                 {
                     controller = "WebPart",    // コントローラ名
                     action = "Index",        // アクション名
                     domain = "",
                 }
            );
            routes.MapRoute(
                "account", "({domain})/Account/{action}",
                 new
                 {
                     controller = "Account",    // コントローラ名
                     action = "Index",        // アクション名
                     domain = "",
                 }
            );

            routes.MapRoute(
                "worktask", "({domain})/t/{action}/{id}/{option}",
                 new
                 {
                     controller = "Worktask",    // コントローラ名
                     action = "Index",        // アクション名
                     id ="",
                     option = "",
                     domain = "",
                 }
            );

            routes.MapRoute(
                "worktask2", "({domain})/t/{action}",
                 new
                 {
                     controller = "Worktask",    // コントローラ名
                     action = "Index",        // アクション名
                     id = "",
                     domain = "",
                 }
            );



            routes.MapRoute(
                "helper", "({domain})/helper/{action}",
                 new
                 {
                     controller = "Helper",    // コントローラ名
                     action = "Index",        // アクション名
                     domain = "",
                 }
            );

            routes.MapRoute(
                "home", "({domain})/home/{action}",
                 new
                 {
                     controller = "home",    // コントローラ名
                     action = "Index",        // アクション名
                     domain = "",
                 }
            );

            routes.MapRoute(
                "settings", "({domain})/Settings/{action}",
                 new
                 {
                     controller = "Settings",    // コントローラ名
                    action = "Configuration",        // アクション名
                    domain = "",
                 }
            );


        }

        private static void RegisisterSingleDomain(RouteCollection routes)
        {

            var config = System.Web.Http.GlobalConfiguration.Configuration;

            var builder = DataController.GetModelBuilder("Data");
            var model = builder.GetEdmModel();

            var odataRouteV4 = config.MapODataServiceRoute(
                routeName: "odataV4",
                routePrefix: "odata",
                model: model);

            var defaultConventions = System.Web.Http.OData.Routing.Conventions.ODataRoutingConventions.CreateDefault();
            var conventions = new List<System.Web.Http.OData.Routing.Conventions.IODataRoutingConvention>();
            foreach (var convention in defaultConventions)
            {
                if (convention is System.Web.Http.OData.Routing.Conventions.MetadataRoutingConvention)
                {
                    // Don't need to special case these conventions.
                    conventions.Add(convention);
                }
                else
                {
                    conventions.Add(new VersionedRoutingConvention(convention, "V3"));
                }
            }

            //var builder3 = DataV3Controller.GetModelBuilder("Data");
            //var odataRoute = config.Routes.MapODataServiceRoute("odataV3", "odatav3", builder3.GetEdmModel(),
            //    new System.Web.Http.OData.Routing.DefaultODataPathHandler(), conventions, new System.Web.Http.OData.Batch.DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));


            routes.MapRoute(
                "files", "f/{*id}",
                 new
                 {
                     controller = "Home",    // コントローラ名
                     action = "Index",        // アクション名
                     domain = "",
                 }
            );
            routes.MapRoute(
                "webparts", "w/{*id}",
                 new
                 {
                     controller = "WebPart",    // コントローラ名
                     action = "Index",        // アクション名
                     domain = "",
                 }
            );
            routes.MapRoute(
                "account", "Account/{action}",
                 new
                 {
                     controller = "Account",    // コントローラ名
                     action = "Index",        // アクション名
                     domain = "common",
                 }

            );

            routes.MapRoute(
                "worktask", "t/{action}/{id}/{option}",
                 new
                 {
                     controller = "WorkTask",    // コントローラ名
                     action = "Index",        // アクション名
                     id = "",
                     option = "",
                     domain = "common",
                 }
            );
            routes.MapRoute(
                "worktask2", "t/{action}",
                 new
                 {
                     controller = "WorkTask",    // コントローラ名
                     action = "Index",        // アクション名
                     id = "",
                     domain = "common",
                 }
            );
            routes.MapRoute(
                "helper", "helper/{action}",
                 new
                 {
                     controller = "Helper",    // コントローラ名
                     action = "Index",        // アクション名
                     domain = "",
                 }
            );


            routes.MapRoute(
                "home", "home/{action}",
                 new
                 {
                     controller = "home",    // コントローラ名
                     action = "Index",        // アクション名
                     domain = "",
                 }
            );
            routes.MapRoute(
                "settings", "Settings/{action}",
                 new
                 {
                     controller = "Settings",    // コントローラ名
                                 action = "Configuration",        // アクション名
                                 domain = "common",
                 }

            );

        }

    }

}
